package controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.BidsDAO;
import dao.ProductsDAO;
import dao.UsersDAO;
import modal.BidModal;
import modal.ProductModal;
import modal.UserModal;

/**
 * Servlet implementation class ProductController
 */
@WebServlet("/ProfileController/*")
public class ProfileController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ProfileController() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request,response);
	}

	protected void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {					
			try {
			HttpSession session = request.getSession();
			int userID = 0;			
			if (session.getAttribute("user") == null){
				response.sendRedirect("/BidMarket/login.jsp");
			}else{
				UserModal user= (UserModal) session.getAttribute("user");
				userID=user.getUserId();
			}
			if(request.getPathInfo()!=null){
				userID = Integer.parseInt(request.getPathInfo().substring(1));		
			}
			UsersDAO uDao = new UsersDAO();
			UserModal user=uDao.getUserwithId(userID); 
			uDao.disconnnect();	
			String email= "Products of "+ user.getEmail();
			
			ProductsDAO pDao = new ProductsDAO();			
			ArrayList<ProductModal> products =new ArrayList<ProductModal>(pDao.getProductwithOwnerId(userID));
			pDao.disconnnect();	
			
			ArrayList<String>myProducts =new ArrayList<String>();
			ArrayList<String>mySoldProducts =new ArrayList<String>();
			for(int i=0; i<products.size(); i++){
				if(products.get(i).getStatus()==0){
					myProducts.add(""+products.get(i).getProductId());
					myProducts.add(products.get(i).getPhotoUrl());
					myProducts.add(products.get(i).getName());
					myProducts.add(products.get(i).getDescription());
					myProducts.add(products.get(i).getPrice());
				}
				else{
					mySoldProducts.add(""+products.get(i).getProductId());
					mySoldProducts.add(products.get(i).getPhotoUrl());
					mySoldProducts.add(products.get(i).getName());
					mySoldProducts.add(products.get(i).getDescription());
					mySoldProducts.add(products.get(i).getPrice());

				}
			}
			
			session.setAttribute("myProducts", myProducts);		
			session.setAttribute("mySoldProducts", mySoldProducts);
			session.setAttribute("email",email);
			response.sendRedirect("/BidMarket/profile.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
